sap.ui.define([
	"sap/ui/core/mvc/Controller",
	"sap/ui/core/routing/History",
	"sap/m/MessageToast",
	"sap/ui/model/json/JSONModel",
	"../../model/formatter"
], (Controller, History, MessageToast, JSONModel, formatter) => {
	"use strict";
	return Controller.extend("ui5.walkthrough.controller.detail.Detail", {
		formatter: formatter,
		onInit() {
			const oViewModel = new JSONModel({
				currency: "EUR"
			});
			this.getView().setModel(oViewModel, "view");

			const oRouter = this.getOwnerComponent().getRouter();
			oRouter.getRoute("detail").attachPatternMatched(this._onObjectMatched, this);
		},

		_onObjectMatched(oEvent) {
			this.byId("idProductRating").reset();

			this.getView().bindElement({
				path: "/" + window.decodeURIComponent(oEvent.getParameter("arguments").invoicePath),
				model: "invoiceRemote"
			});
		},

		onButtonshowModelPress() {
			const oRouter = this.getOwnerComponent().getRouter();
			oRouter.navTo("param", {
				param1: "param1",
				param2: "param2"
			});
		},

		onPageNavButtonPress() {
			const oHistory = History.getInstance();
			const sPreviousHash = oHistory.getPreviousHash();

			if (sPreviousHash !== undefined) {
				window.history.go(-1);
			} else {
				const oRouter = this.getOwnerComponent().getRouter();
				oRouter.navTo("overview", {});
				// 没有上一画面时，跳转到overview画面
			}
		},

		onProductRatingChange(oEvent) {
			const fValue = oEvent.getParameter("value");
			const oResourceBundle = this.getView().getModel("i18n").getResourceBundle();

			MessageToast.show(oResourceBundle.getText("ratingConfirmation", [fValue]));
		},

		onLogButtonPress: function(oEvent) {
			console.log(this.getOwnerComponent().getModel("invoiceRemote").oData);
			console.log(Object.values(this.getOwnerComponent().getModel("invoiceRemote").oData)[0].ShippedDate);
		}
	});
});